<template>
  <div class="m-container">
    <el-divider content-position="left">
      <el-button-group>
        <el-button type="primary" icon="el-icon-plus" @click="addDeptClick">新增</el-button>
        <el-button type="primary" icon="el-icon-refresh" @click="getTableData">刷新</el-button>
      </el-button-group>
    </el-divider>
    <el-table :data="tableData" style="width: 100%" height="90%" v-loading="loading">
      <el-table-column type="index" label="序号" width="100"> </el-table-column>
      <el-table-column prop="name" label="部门名称" width="180"></el-table-column>
      <!-- <el-table-column prop="staffCount" label="员工人数" width="180"></el-table-column>
      <el-table-column prop="cadreCount" label="干部人数" width="180"></el-table-column> -->
      <el-table-column prop="meritPay" label="绩效余额" width="180"></el-table-column>
      <el-table-column fixed="right" label="操作">
        <template #default="scope">
          <el-button @click="editDeptClick(scope.row)" type="text" size="small">编辑</el-button>
          <el-button type="text" size="small" @click="deleteDeptClick(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>    
  </div>
  <!-- 添加部门对话框 -->
  <el-dialog :title="部门绩效" v-model="dlgDept" width="460px">
    <div>
      <el-form :model="formDeptWage" :rules="rules" ref="formDept" label-width="80px">
        <el-form-item label="所属部门" prop="id">
          <el-select v-model="formDeptWage.id" placeholder="请选择部门" @change="getDeptStaff">
            <el-option v-for="item in depts" :key="item.id" :label="item.name" :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="绩效余额" prop="meritPay">
          <el-input type="number" v-model.number="formDeptWage.meritPay"></el-input>
        </el-form-item>
        <el-form-item label="工资员" prop="clerk">
          <el-select v-model="formDeptWage.clerkId" placeholder="请选择" clearable>
            <el-option v-for="item in staff" :key="item.id" :label="item.name" :value="item.id"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
    </div>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="dlgDept = false">取 消</el-button>
        <el-button type="primary" @click="saveDeptClick">确 定</el-button>
      </span>
    </template>
  </el-dialog>
</template>
<script lang="ts">
import { defineComponent, reactive } from "vue";
import { getDepts, getDeptWages, saveDeptWage, deleteDeptWage, getUsersOfDept } from "@/api/salary";
import { ElMessageBox, ElMessage } from "element-plus";
import { cloneObject } from '@/api/utils'
export default defineComponent({
  name: "DeptWage",
  setup(props) {
    const emptyForm = reactive({
      id: '',            // 编号
      meritPay: 0,      // 绩效余额
      clerkId: ''        // 工资员
    });
    return {
      emptyForm,
    };
  },
  data() {
    return {
      tableData: [],
      dlgDept: false,
      formDeptWage: {
        id: '',            // 编号
        meritPay: 0,      // 绩效余额
        clerkId: ''        // 工资员
      },
      rules: {
        id: [
          { required: true, message: "请选择部门", trigger: "blur" }
        ],
        name: [
          { required: true, message: "请输入部门名称", trigger: "blur" },
          { min: 3, max: 12, message: "长度在 3 到 12 个字符", trigger: "blur", },
        ],
        totalWage: [
          { required: true, message: "请输入部门名称", trigger: "blur" },
          { type: 'number', min: 0, max: 100000, message: "绩效总额不正确", trigger: "blur", }
        ]
      },
      depts: [],
      staff: [],
      loading: false
    };
  },
  created() {
    this.getTableData();
    getDepts().then(res => {
      this.depts = res.data
    })
  },
  methods: {
    // 获取表格数据
    getTableData() {
      getDeptWages().then((res) => {
        this.tableData = res.data
        console.log(res.data)
      });
    },
    // 获取部门员工列表
    getDeptStaff(deptId: number) {
      getUsersOfDept(deptId).then(res => {
        this.staff = res.data
      })
    },
    resetForm() {
      this.formDeptWage = this.emptyForm;
      (this.$refs["formDept"] as any)?.resetFields()
    },
    // 添加部门信息
    addDeptClick() {
      this.resetForm()
      this.dlgDept = true
    },
    // 编辑部门信息
    editDeptClick(row: any) {
      (this.$refs["formDept"] as any)?.resetFields()  // 重置验证信息
      this.formDeptWage = cloneObject(row)
      getUsersOfDept(this.formDeptWage.id as any).then(res => {
        this.staff = res.data
        this.dlgDept = true
      })
    },
    // 保存部门信息
    saveDeptClick() {
      (this.$refs["formDept"] as any).validate((valid: boolean) => {
        if (valid) {
          this.loading = true
          saveDeptWage(this.formDeptWage).then((res) => {
            let rs = res as any
            if(rs.code === 20000) {
              ElMessage.success("保存成功!")
              this.getTableData()
            }
            else{
              console.log(rs)
              ElMessage.error(rs.message)
            }
            this.dlgDept = false;
          })
          .finally(()=>{
            this.loading = false
          });
        }
      });
    },
    // 删除部门信息
    deleteDeptClick(id: number) {
      ElMessageBox.confirm("确认删除该部门绩效设置?","提示",{
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          deleteDeptWage(id).then(res => {
            if(res.data === true) {
              this.getTableData()
              ElMessage.success("删除成功")
            }
            else {
              ElMessage.warning("删除失败")
            }
          })
        }).catch(() => {
          ElMessage.info("删除错误")
        })
    }
  },
});
</script>
<style scoped>
</style>
